package com.taptech.doufu.ui.dialog;


import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.taptech.doufu.bean.cp.CpBean;
import com.taptech.doufu.R;
import com.taptech.doufu.ui.activity.NovelCreateTosActivity;
import com.taptech.doufu.ui.activity.weex.SimpleWeexActivity;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.bean.personalcenter.PersonalDraftInfo;
import com.taptech.doufu.util.AnimationUtil;
import com.taptech.doufu.util.TMAnalysis;

public class CpAddContentDialog extends Dialog implements android.view.View.OnClickListener
{

    public static final int TYPE_FRAGMENT = 1;
    public static final int TYPE_CPDES = 2;
    private int type = 1;
    ImageView homeClose;
    TextView alphaView;
    Context context;
    private ImageView homeDrawImg, homeNovelImg, homeNoteImg;
    private RelativeLayout main_home_layout;
    private int novelNum = 0;
    private Window window;
    private View line;


    public CpAddContentDialog(Context context)
    {
        super(context);
        // TODO Auto-generated constructor stub
        this.context = context;
    }

    public CpAddContentDialog(Context context, int theme)
    {
        super(context, theme);
        // TODO Auto-generated constructor stub
        this.context = context;
    }

    public CpAddContentDialog(Context context, int theme, int type)
    {
        super(context, theme);
        this.context = context;
        this.type = type;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.popup_cp_add_content);
        initDrawAndCos();
        setOnDismissListener(new OnDismissListener()
        {
            @Override
            public void onDismiss(DialogInterface dialogInterface)
            {
                setBackgroundAlpha(1f);
            }
        });
        showUgcSweepView();
    }

    @Override
    public void show()
    {
        super.show();
        setBackgroundAlpha(0.4f);
    }

    public void setBackgroundAlpha(float bgAlpha)
    {
        WindowManager.LayoutParams lp = ((Activity) context).getWindow().getAttributes();
        lp.alpha = bgAlpha;
        ((Activity) context).getWindow().setAttributes(lp);
    }

    private void initDrawAndCos()
    {
        line = (View) findViewById(R.id.line);
        main_home_layout = (RelativeLayout) findViewById(R.id.main_home_layout);
        main_home_layout.setOnClickListener(this);
        findViewById(R.id.home_draw_layout).setOnClickListener(this);
        findViewById(R.id.home_novel_layout).setOnClickListener(this);
        findViewById(R.id.home_note_layout).setOnClickListener(this);
        homeDrawImg = (ImageView) findViewById(R.id.main_home_draw_img);
        homeNovelImg = (ImageView) findViewById(R.id.main_home_novel_img);
        homeNoteImg = (ImageView) findViewById(R.id.main_home_note_img);
        homeClose = (ImageView) findViewById(R.id.main_close);
        homeClose.setOnClickListener(this);
        switch (type)
        {
            case TYPE_FRAGMENT:
                line.setVisibility(View.VISIBLE);
                break;
            case TYPE_CPDES:
                line.setVisibility(View.GONE);
                break;
            default:
                break;
        }
    }

    protected void showUgcSweepView()
    {
        AnimationUtil.starShockLoopAnimationLong(homeNovelImg, 0.85f, 100);
        AnimationUtil.starShockLoopAnimationLong(homeDrawImg, 0.85f, 150);
        AnimationUtil.starShockLoopAnimationLong(homeNoteImg, 0.85f, 200);


    }

    public void showDialog()
    {
        window = getWindow(); //得到对话框
        window.setWindowAnimations(R.style.dialogWindowAnim); //设置窗口弹出动画
        window.setBackgroundDrawableResource(R.color.transparent); //设置对话框背景为透明
        show();
    }

    @Override
    public void onClick(View v)
    {
        // TODO Auto-generated method stub
        switch (v.getId())
        {
            case R.id.main_home_layout:
                dismiss();
                break;
            case R.id.home_novel_layout:
            {
                TMAnalysis.event(context, "myPost_newNovel");
                if (cpBean != null)
                {
//                    Intent intent = new Intent(v.getContext(), NovelCreateActivity.class);
                    Intent intent = new Intent(v.getContext(), NovelCreateTosActivity.class);
                    intent.putExtra(Constant.TAG, cpBean);
                    v.getContext().startActivity(intent);

                } else
                {

                    Intent intent = new Intent(v.getContext(), NovelCreateTosActivity.class);
                    v.getContext().startActivity(intent);

                }
                dismiss();
            }
            break;
            case R.id.home_note_layout:
            {
                TMAnalysis.event(context, "tag_or_cp_jump_note");
                Intent intent = new Intent(v.getContext(), SimpleWeexActivity.class);
                intent.putExtra(SimpleWeexActivity.INTENT_WEEX_URL_KEY,"vue/find/TFTopicPublish.js?needLogin=1");
                initTag(intent);
                v.getContext().startActivity(intent);
                dismiss();
            }
            break;
            case R.id.main_close:
                dismiss();
                break;
            default:
                break;
        }
    }

    private void initTag(Intent intent)
    {
        if (cpBean == null)
        {
            return;
        }
        PersonalDraftInfo personalDraftInfo = new PersonalDraftInfo();
        personalDraftInfo.setCpBean(cpBean);
        intent.putExtra(DRFT_BEAN, personalDraftInfo);
    }

    public static final String DRFT_BEAN = "draft_bean";
    private CpBean cpBean;


    public void setTagString(CpBean cpBean)
    {
        this.cpBean = cpBean;
    }
}
